/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Threading;

namespace myBoard.Tools
{
    public enum AnimationType
    {
        Linear, AccelerationDeceleration
    }

    public class Animator
    {
        private List<MotionElement> _Elements;
        private DispatcherTimer timer;
        private TimeSpan _Duration;
        public delegate void OnNewValue(List<MotionElement> elements);
        public event OnNewValue NewValue;
        public delegate void OnAnimationCompleted(List<MotionElement> elements);
        public event OnAnimationCompleted AnimationCompleted;
        public DateTime lastUpdate;
        double elapsedTime;
        private double _Progression;
        bool firstTick;
        private int _fps = 100;
        private AnimationType _animatorAnimationType;

        #region Properties

        public List<MotionElement> Elements
        {
            get { return _Elements; }
            set { _Elements = value; }
        }

        public TimeSpan Duration
        {
            get { return _Duration; }
            set { _Duration = value; }
        }

        public int FramePerSecond
        {
            get { return _fps; }
            set { _fps = value; }
        }

        public double Progression
        {
            get { return _Progression; }
            set { _Progression = value; }
        }

        public AnimationType AnimatorAnimationType
        {
            get { return _animatorAnimationType; }
            set { _animatorAnimationType = value; }
        }

        #endregion

        public Animator(TimeSpan duration, int fps)
        {
            _fps = fps;
            _Duration = duration;
            Init();
        }

        public Animator(TimeSpan duration)
        {
            _Duration = duration;
            Init();
        }

        private void Init()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, (1000 / _fps));
            timer.Tick += new EventHandler(timer_Tick);
            _Elements = new List<MotionElement>();
            timer.IsEnabled = false;
            lastUpdate = new DateTime();
            _animatorAnimationType = AnimationType.AccelerationDeceleration;
        }

        public void Start()
        {
            if (_Elements.Count > 0)
            {
                elapsedTime = 0;
                firstTick = true;
                timer.IsEnabled = true;
            }
            else
                if (AnimationCompleted != null)
                {
                    timer.Stop();
                    AnimationCompleted(_Elements);
                }
        }

        public void Stop()
        {
            timer.Stop();
        }

        public void Resume()
        {
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (firstTick)
            {
                lastUpdate = DateTime.Now;
                firstTick = false;
            }

            foreach (MotionElement element in _Elements)
            {
                elapsedTime += ((TimeSpan)(DateTime.Now - lastUpdate)).TotalMilliseconds;
                _Progression = (elapsedTime / _Duration.TotalMilliseconds) * 100;
                if (elapsedTime >= _Duration.TotalMilliseconds)
                {
                    element.CurrentValue = element.EndValue;
                    timer.IsEnabled = false;
                    if (NewValue != null)
                        NewValue(_Elements);
                    if (AnimationCompleted != null)
                    {
                        timer.Stop();
                        AnimationCompleted(_Elements);
                    }
                    return;
                }

                double f = 0;
                if (_animatorAnimationType == AnimationType.AccelerationDeceleration)
                    f = (((Math.Sin(Math.PI * (elapsedTime / _Duration.TotalMilliseconds) - (Math.PI / 2)) + 1) / 2) * (element.EndValue - element.StartValue));
                else if (_animatorAnimationType == AnimationType.Linear)
                    f = (elapsedTime / _Duration.TotalMilliseconds) * (element.EndValue - element.StartValue);


                element.CurrentValue = f + element.StartValue;
                lastUpdate = DateTime.Now;
            }

            if (NewValue != null)
                NewValue(_Elements);

            //System.Diagnostics.Debug.WriteLine(_Elements[0].StartValue.ToString()+ "->" + _Elements[0].EndValue.ToString()+ " : " + _Elements[0].CurrentValue.ToString());
        }

        public MotionElement GetElement(string name)
        {
            foreach (MotionElement element in _Elements)
            {
                if (element.Name == name)
                    return element;
            }
            return null;
        }

        public void RemoveElement(string name)
        {
            for (int i = 0; i < _Elements.Count; i++)
            {
                MotionElement element = _Elements[i];
                if (element.Name == name)
                {
                    _Elements.Remove(element);
                    break;
                }
            }

        }

        public void AddElements(List<MotionElement> elements)
        {
            _Elements = elements;
        }
    }

    #region MotionElement

    public class MotionElement
    {
        double _StartValue;
        double _EndValue;
        double _CurrentValue;
        List<double> _Steps;
        string _Name;

        #region Properties

        public double StartValue
        {
            get { return _StartValue; }
            set
            {
                _StartValue = value;
                _CurrentValue = value;
            }
        }

        public double EndValue
        {
            get { return _EndValue; }
            set { _EndValue = value; }
        }

        public double CurrentValue
        {
            get { return _CurrentValue; }
            set { _CurrentValue = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public List<double> Steps
        {
            get { return _Steps; }
            set { _Steps = value; }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">Name of the value you want to modify</param>
        /// <param name="start">Start value</param>
        /// <param name="end">Target value</param>

        public MotionElement(string name, double start, double end)
        {
            _Name = name;
            _StartValue = start;
            _EndValue = end;
            _Steps = new List<double>();
        }
    }

    #endregion

}

